package spotGUI.personalProfileGUI;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.tomahawk.XFileDialog;
import personalProfile.FamilyManager;
import utils.MaxLenghtTextDocument;
import java.util.ResourceBundle;

public class AddFamilyComponentPanel extends JPanel {

	private static final String[] RELATIVES = {"Amico", "Genitore", "Nonno", "Cugino", "Zio", "Fidanzato"};
	
	private JButton btnPhoto;
	private JLabel lblName;
	private JTextField textFieldName;
	private JLabel lblType;
	private JComboBox boxType;
	private JButton btnAddParent;
	private JLabel lblAddParent;

	private XFileDialog fileChooser = new XFileDialog("Scegli immagine");
	private String pendingParentPhoto = null;
	private JLabel lblPhotoLoaded;
	private JLabel lblRemove;

	public AddFamilyComponentPanel (final FamilyManager manager, FamilyPanel panel) {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{120, 32, 128, 8, 125, 35, 14, 35, 0};
		gridBagLayout.rowHeights = new int[]{24, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);



		lblAddParent = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.lblAddParent.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblAddParent.setFont(new Font("Agency FB", Font.BOLD | Font.ITALIC, 18));
		GridBagConstraints gbc_lblAddParent = new GridBagConstraints();
		gbc_lblAddParent.fill = GridBagConstraints.BOTH;
		gbc_lblAddParent.insets = new Insets(0, 0, 5, 5);
		gbc_lblAddParent.gridx = 0;
		gbc_lblAddParent.gridy = 0;
		add(lblAddParent, gbc_lblAddParent);

		lblName = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.lblName.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 0;
		add(lblName, gbc_lblName);

		textFieldName = new JTextField();
		textFieldName.setDocument(new MaxLenghtTextDocument(50));
		textFieldName.setBackground(UIManager.getColor("Desktop.background"));
		GridBagConstraints gbc_textFieldName = new GridBagConstraints();
		gbc_textFieldName.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldName.insets = new Insets(0, 0, 5, 5);
		gbc_textFieldName.gridx = 2;
		gbc_textFieldName.gridy = 0;
		add(textFieldName, gbc_textFieldName);
		textFieldName.setColumns(10);

		lblType = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.lblType.text")); //$NON-NLS-1$ //$NON-NLS-2$

		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.anchor = GridBagConstraints.EAST;
		gbc_lblType.insets = new Insets(0, 0, 5, 5);
		gbc_lblType.gridx = 3;
		gbc_lblType.gridy = 0;
		add(lblType, gbc_lblType);

		boxType = new JComboBox();
		boxType.setBackground(UIManager.getColor("Desktop.background"));
		boxType.setModel(new DefaultComboBoxModel(RELATIVES));
		boxType.setSelectedIndex(0);
		GridBagConstraints gbc_boxType = new GridBagConstraints();
		gbc_boxType.fill = GridBagConstraints.HORIZONTAL;
		gbc_boxType.insets = new Insets(0, 0, 5, 5);
		gbc_boxType.gridx = 4;
		gbc_boxType.gridy = 0;
		add(boxType, gbc_boxType);

		btnAddParent = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.btnAddParent.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnAddParent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manager.addPendingComponent(boxType.getSelectedIndex()+1, textFieldName.getText(), pendingParentPhoto);
				reset();
			}
		});

		btnPhoto = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.btnPhoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnPhoto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String fileName = fileChooser.getFile();
				String directory = fileChooser.getDirectory();
				pendingParentPhoto = directory+fileName;
				lblPhotoLoaded.setVisible(true);
			}
		});
		
		lblPhotoLoaded = new JLabel("");
		lblPhotoLoaded.setIcon(new ImageIcon(AddFamilyComponentPanel.class.getResource("/images/Icons/24x24px/accept.png")));
		lblPhotoLoaded.setVisible(false);
		lblPhotoLoaded.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.lblPhotoLoaded.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblPhotoLoaded = new GridBagConstraints();
		gbc_lblPhotoLoaded.fill = GridBagConstraints.VERTICAL;
		gbc_lblPhotoLoaded.insets = new Insets(0, 0, 5, 5);
		gbc_lblPhotoLoaded.gridx = 5;
		gbc_lblPhotoLoaded.gridy = 0;
		add(lblPhotoLoaded, gbc_lblPhotoLoaded);
		
		
		GridBagConstraints gbc_btnPhoto = new GridBagConstraints();
		gbc_btnPhoto.anchor = GridBagConstraints.NORTH;
		gbc_btnPhoto.insets = new Insets(0, 0, 5, 5);
		gbc_btnPhoto.gridx = 6;
		gbc_btnPhoto.gridy = 0;
		add(btnPhoto, gbc_btnPhoto);
		GridBagConstraints gbc_btnAddParent = new GridBagConstraints();
		gbc_btnAddParent.insets = new Insets(0, 0, 5, 0);
		gbc_btnAddParent.anchor = GridBagConstraints.NORTH;
		gbc_btnAddParent.gridx = 7;
		gbc_btnAddParent.gridy = 0;
		add(btnAddParent, gbc_btnAddParent);
		
		lblRemove = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddFamilyComponentPanel.lblRemove.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblRemove = new GridBagConstraints();
		gbc_lblRemove.anchor = GridBagConstraints.NORTH;
		gbc_lblRemove.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblRemove.gridwidth = 8;
		gbc_lblRemove.insets = new Insets(0, 0, 0, 5);
		gbc_lblRemove.gridx = 0;
		gbc_lblRemove.gridy = 1;
		add(lblRemove, gbc_lblRemove);
	}
	
	private void reset () {
		pendingParentPhoto = null;
		lblPhotoLoaded.setVisible(false);
		textFieldName.setText(null);
	}
}
